import '../sass/common.scss';
import * as tools from './tools';

let sendCode = tools.initSendCode($('#sendCode'));
$(function(){
    sessionStorage.setItem('loginPage', 'index');
    $('#sendCode').click(function(){
        if(!tools.checkInput($('#phone')[0])) return
        $('#sendCode').addClass('sent');
        sendCode({
            cb() {
                //do ajax
                $.ajax({
                    url : tools.axUrl + 'method=smsValidationCode&params=&appKey=OPEN_PLATFORM_APPKEY&v=1.0',
                    type : 'post',
                    data : {
                        mobile : $('#phone').val()
                    },
                    success : function(res){
                    }
                })
            }
        })
    });

    $('input').on('blur', function (event) {
        let trg = event.target;
    });

    //处理提交最后一次校验
    $('.handleSubmit').click(function() {
        let inputs = $('input[id]');
        let result = true;
        for(let i = 0; i < inputs.length - 1; i++) {
            result = tools.checkInput(inputs[i])
        }
        if(result && $('#majic')[0].checked) {
            doRegister();
        }
        if(!$('#majic')[0].checked) {
            alert('请阅读用户协议!')
        }
    })
})

async function doRegister() {
    let res1 = await phoneCheck();
    let res2 = await emailCheck();
    if(res1.data != null) {
        alert('手机号已被注册!');
        return
    }
    if(res2.data != null) {
        alert('邮箱已被注册!')
        return
    }

    if(res1.data == null && res2.data == null) {
        let res3 = await registerSave();
        if(res3.code == '0000') {
            alert('注册成功，正在为您跳转到登录页面');
            setTimeout(function() {
                window.location.href = './login.html';
            }, 1500)
        }else {
            alert(res3.message)
        }
    }
}

function registerSave() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url : tools.axUrl + 'method=saveUser&params=&appKey=OPEN_PLATFORM_APPKEY&v=1.0',
            type : 'post',
            data : {
                mobile : $('#phone').val(),
                loginName : $('#userName').val(),
                loginPwd : $('#password').val(),
                email : $('#email').val(),
                smsCode : $('#code').val()
            },
            success : function(res) {
                resolve(res);
            }
        })
    })
}


function phoneCheck() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url : tools.axUrl + 'method=getAccountByMobile&params=&appKey=OPEN_PLATFORM_APPKEY&v=1.0',
            type : 'post',
            data : {
                mobile : $('#phone').val()
            },
            success : function(res) {
                resolve(res);
            }
        })
    })
    
}

function emailCheck() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url : tools.axUrl + 'method=getAccountByEmail&params=&appKey=OPEN_PLATFORM_APPKEY&v=1.0',
            type : 'post',
            data : {
                email : $('#email').val()
            },
            success : function(res) {
                resolve(res);
            }
        })
    })
   
}